//
//  CZStatusPictureView.swift
//  weiBo
//
//  Created by apple on 16/5/16.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

//每项的间距
let itemMargin:CGFloat = 5
//计算每一个图片的宽度
let width = (UIScreen.mainScreen().bounds.size.width - itemMargin * 2 - StatusTableViewCellMargin * 2) / 3

class CZStatusPictureView: UICollectionView,UICollectionViewDataSource {
    //拦截在图片数量的label
    private lazy var picCountLabel: UILabel = {
        let label = UILabel(textColor: UIColor.blackColor(), fontSize: 15)
        
        return label
    }()
    //绑定数据模型
    
    var picUrls : [CZStatusPictureInfo]? {
        didSet{
            let currentSize = calcSize(picUrls?.count ?? 0)
            picCountLabel.text = "\(picUrls?.count ?? 0 )"
            
            self.snp_makeConstraints { (make) -> Void in
                make.size.equalTo(currentSize)
                
            }
            
            reloadData()
        }
    }

    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        let flowLayOut = UICollectionViewFlowLayout()

        super.init(frame: frame, collectionViewLayout: flowLayOut)

        
        //设置item的大小
        flowLayOut.itemSize = CGSizeMake(width, width)
        //设置水平间距
        flowLayOut.minimumInteritemSpacing = itemMargin
        //设置垂直间距
        flowLayOut.minimumLineSpacing = itemMargin
        
        
        setUpUI()
    }

    
    
    
    private func setUpUI(){
        
        //注册cell
        registerClass(CZStatusPictureViewCell.self, forCellWithReuseIdentifier: "cell")
        dataSource = self
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //根据图片个数生成一个CGSize
    private func calcSize(count: Int) -> CGSize{
        //计算列数
        let col = count > 3 ? 3 : count
        //计算行数
        let row = (count - 1) / 3 + 1
        //计算宽度
        let currentWidth =  CGFloat(col) * width + CGFloat(col - 1) * itemMargin
        //计算高度
        let currentHeight = width * CGFloat(row) + CGFloat(row - 1) * itemMargin
        
        return CGSize(width: currentWidth, height: currentHeight)
    
    }
    
    
    // MARK  collectionView的数据源方法
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return picUrls?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! CZStatusPictureViewCell
        cell.pictInfo = picUrls![indexPath.item]
        
        return cell
    }
}

//自定义cell
class CZStatusPictureViewCell: UICollectionViewCell {
    //接受外界传来的数据
    var pictInfo: CZStatusPictureInfo? {
        didSet{
            guard let url = pictInfo?.thumbnail_pic else {
                return
            }
            iconImage.sd_setImageWithURL(NSURL(string: url), placeholderImage: UIImage(named: "timeline_image_placeholder"))
            
            gifImage.hidden = !url.hasSuffix(".gif")
            
        }
    }

    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    //懒加载图片
    private lazy var iconImage: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "timeline_image_placeholder"))
        imageView.contentMode = UIViewContentMode.ScaleAspectFill
        imageView.clipsToBounds = true
        
        return imageView
    }()
    //懒加载gif图片
    private lazy var gifImage: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "timeline_image_gif"))
        
        return imageView
    }()
    private func setUpUI(){
        contentView.addSubview(iconImage)
        contentView.addSubview(gifImage)
        
        iconImage.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        
        gifImage.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(iconImage)
            make.bottom.equalTo(iconImage)
        }
   
    }
}










